﻿using DreamCMS.BLL.Factory;
using DreamCMS.BLL.Factory.Interface;
using DreamCMS.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DreamCMS.BLL
{
    /// <summary>
    /// 统计管理
    /// </summary>
    public class StatisticManager
    {
        private static IStatisticDAO dao = DataAccessFactory.CreateStatisticDAO();
        private static object lockHelper = new object();
        static StatisticManager()
        {
           if(statistic==null)
           {
               lock(lockHelper)
               {
                   if(statistic==null)
                   {
                       statistic = dao.GetStatistic();
                   }
               }
           }
        }

        private static StatisticInfo statistic;
        /// <summary>
        /// 清空缓存
        /// </summary>
        public static void ClearCache()
        {
             statistic = dao.GetStatistic();
        }


        /// <summary>
        /// 获取统计信息实体
        /// </summary>
        /// <returns></returns>
        public static StatisticInfo GetStatisticInfo()
        {
            return statistic;
        }
        /// <summary>
        /// 更新数据统计信息到数据库
        /// </summary>
        /// <returns></returns>
        public static bool UpdateStatisticInfo()
        {
            return dao.UpdateStatistic(statistic);
        }

        public static bool UpdateStatisticPostCounts(long addCount)
        {
            statistic.PostCounts += addCount;
            if(dao.UpdateStatistic(statistic))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 更新评论数
        /// </summary>
        /// <param name="addCount">增加数，可为负数</param>
        /// <returns></returns>
        public static bool UpdateStatisticsCommentCount(long addCount)
        {
            statistic.CommentCounts += addCount;
            return UpdateStatisticInfo();
        }

    }
}
